package up.android;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ViewsListAdapter extends BaseAdapter implements OnClickListener 
{
	private Context ctx;
	private View selView=null;
	private OnClickListener onClickListenerDelegate;
	
	public ViewListItem getSelectedProblem()
	{
		if(selView != null)
			return (ViewListItem)selView.getTag();
		else
			return null;
	}
	
	public ViewsListAdapter(Context ctx)
	{
		this.ctx = ctx;
	}
	public int getCount() {
		return ViewListItem.values().length;
	}

	public Object getItem(int position) {
		return ViewListItem.values()[position];
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		View aux;
		
		if(convertView == null)
		{
			LayoutInflater inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			aux = inflater.inflate(R.layout.listitem, null);
		}
		else
		{
			aux = convertView;
		}
		
		ViewListItem p = ViewListItem.values()[position];
		
		TextView label = (TextView)aux.findViewById(R.id.itemLabel);
		label.setText(p.label());
		label = (TextView)aux.findViewById(R.id.itemDesc);
		label.setText(p.description());
		
		ImageView imgView = (ImageView)aux.findViewById(R.id.imageView1);
		imgView.setImageResource(p.image());
		
		aux.setTag(p);
		aux.setOnClickListener(this);
		return aux;
	}

	public void setOnClickListener(OnClickListener onClickListener) {
		onClickListenerDelegate = onClickListener;
	}

	public void onClick(View v) {
		if(onClickListenerDelegate != null)
			onClickListenerDelegate.onClick(v);
	}
	
}
